package com.example.thread;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Quincy on 2018/10/12.
 */
public class MultiThreadServer implements Runnable {
    Socket csocket;
    MultiThreadServer(Socket csocket){
        this.csocket = csocket;
    }

    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = new ServerSocket(1234);
        System.out.println("Listening");
        while (true){
            Socket socket = serverSocket.accept();
            System.out.println("Connected");
            new Thread(new MultiThreadServer(socket)).start();
        }
    }
    @Override
    public void run() {
        try {
            PrintStream printStream = new PrintStream(csocket.getOutputStream());
            for (int i = 100; i >= 0; i--){
                printStream.println(i);
            }
            printStream.close();
            csocket.close();
        }
        catch (IOException e){
            System.out.print(e);
        }
    }
}
